from java.lang import *
from java.io import *
from jarray import *
from ibis.mpj import *
from no.uib.cipr.matrix import *

System.setProperty("ibis.name_server.host", "10.0.0.1")

System.setProperty("ibis.name_server.key", "mpjtest2")
System.setProperty("ibis.pool.total_hosts", "2")

System.setProperty("ibis.connect.enable", "true")
System.setProperty("ibis.connect.data_links", "RoutedMessages")
System.setProperty("ibis.connect.control_links", "RoutedMessages")
# System.setProperty("ibis.connect.hub_host", "10.0.0.1")

MPJ.init([])
size = MPJ.COMM_WORLD.size()
rank = MPJ.COMM_WORLD.rank()
print rank
print size

intervals = zeros(1, 'd')

if (rank == 0):
    intervals[0] = 1000

MPJ.COMM_WORLD.bcast(intervals, 0, 1, MPJ.INT, 0)

n = intervals[0]
h = 1.0 / n
sum = 0.0
for i in range(rank, n, size):
    x = h * (i + 0.5)
    sum += 4.0 /(1.0 + x * x)
mypi = array([h * sum], 'd')
pi = array([0.0], 'd')

MPJ.COMM_WORLD.reduce(mypi, pi, 1, MPJ.DOUBLE, MPJ.SUM, 0, MPJ.COMM_WORLD)

if (rank == 0):
    print pi[0]
    
MPJ.finish()
